<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      v-show="showSearch"
      :inline="true"
    >
      <el-form-item label="类别代码" prop="lbmLbdm">
        <el-input
          v-model="queryParams.lbmLbdm"
          placeholder="请输入类别代码"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="类别名称" prop="lbmLbmc">
        <el-input
          v-model="queryParams.lbmLbmc"
          placeholder="请输入类别名称"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="openSelectUser"
          v-hasPermi="['project:customer:addLbm']"
        >
          添加类别
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-circle-close"
          size="mini"
          :disabled="multiple"
          @click="cancelAuthUserAll"
          v-hasPermi="['project:customer:cancelLbm']"
        >
          批量取消授权
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-close"
          size="mini"
          @click="handleClose"
          >关闭</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="lbmList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        label="类别代码"
        prop="lbmLbdm"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="类别名称"
        prop="lbmLbmc"
        :show-overflow-tooltip="true"
      />
      <el-table-column label="状态" align="center" prop="lbmFlag">
        <template #default="scope">
          <dict-tag :options="statusOptions" :value="scope.row.lbmFlag" />
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-circle-close"
            @click="cancelAuthLbm(scope.row)"
            v-hasPermi="['project:customer:cancleSingleLbm']"
          >
            取消授权
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <select-lbm ref="select" :cusId="queryParams.cusId" @ok="handleQuery" />
  </div>
</template>

<script>
import {
  allocatedLbmList,
  authLbmCancel,
  authLbmCancelAll,
} from "@/api/project/customer";
import selectLbm from "./selectLbm";

export default {
  name: "cusLbm",
  components: { selectLbm },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 类别组
      lbmIds: [],
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户表格数据
      lbmList: [],
      // 状态数据字典
      statusOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        cusId: undefined,
        lbmLbdm: undefined,
        lbmLbmc: undefined,
      },
    };
  },
  created() {
    const cusId = this.$route.params && this.$route.params.cusId;
    if (cusId) {
      this.queryParams.cusId = cusId;
      this.getList();
      this.getDicts("sys_normal_disable").then((response) => {
        this.statusOptions = response.data;
      });
    }
  },
  methods: {
    /** 查询授权用户列表 */
    getList() {
      this.loading = true;
      allocatedLbmList(this.queryParams).then((response) => {
        this.lbmList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 返回按钮
    handleClose() {
      // 返回上级路由并关闭当前路由
      this.$store.state.tagsView.visitedViews.splice(
        this.$store.state.tagsView.visitedViews.findIndex(
          (item) => item.path === this.$route.path
        ),
        1
      );
      this.$router.back();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.lbmIds = selection.map((item) => item.lbmId);
      this.multiple = !selection.length;
    },
    /** 打开授权类别代码弹窗 */
    openSelectUser() {
      this.$refs.select.showLbm(this.queryParams.cusId);
    },
    /** 取消授权按钮操作 */
    cancelAuthLbm(row) {
      const cusId = this.queryParams.cusId;
      this.$confirm('确认要取消该"' + row.lbmLbmc + '"类别吗？', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return authLbmCancel({ lbmId: row.lbmId, cusId: cusId });
        })
        .then(() => {
          this.getList();
          this.msgSuccess("取消授权成功");
        })
        .catch(() => {});
    },
    /** 批量取消授权按钮操作 */
    cancelAuthUserAll(row) {
      const cusId = this.queryParams.cusId;
      const lbmIds = this.lbmIds.join(",");
      this.$confirm("是否取消选中用户授权数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          return authLbmCancelAll({ cusId: cusId, lbmIds: lbmIds });
        })
        .then(() => {
          this.getList();
          this.msgSuccess("取消授权成功");
        })
        .catch(() => {});
    },
  },
};
</script>
